﻿package  
{
	import com.urbansquall.ginger.*;
	import flash.display.*;
	import flash.events.*;
	import flash.utils.*;
	
	public class FoxDropMain extends Sprite
	{	
		private static const TIMER_INTERVAL : Number = 15;
		
		private var m_timer : Timer;
		private var m_animationPlayer: AnimationPlayer;
		private var m_animationIndex : int = 0;
		
		public function FoxDropMain() 
		{
			loadDefinition();
			
			m_timer = new Timer( TIMER_INTERVAL );
			m_timer.addEventListener( TimerEvent.TIMER, update );
			m_timer.start();
			
			this.addEventListener( MouseEvent.MOUSE_UP, mouseUp );
		}
		
		private function loadDefinition() : void
		{
			m_animationPlayer = AnimationPlayerBuilder.parse( XMLLibrary.FOXDROP_XML, new BitmapDataLibrary() );			

			m_animationPlayer.x = 400;
			m_animationPlayer.y = 300;
			addChild( m_animationPlayer );	
			
			m_animationPlayer.addEventListener( FrameEvent.CHANGE, handleFrameChange );
			m_animationPlayer.addEventListener( AnimationEvent.CHANGE, handleAnimationChange );
		}
		
		private function update( a_event : Event ):void 
		{
			m_animationPlayer.update( TIMER_INTERVAL );
		}		
		
		private function mouseUp( a_event : Event ):void 
		{
			playNextAnimation();
		}		
		
		private function playNextAnimation() : void
		{
			m_animationIndex++;
			var animationIDs : Array = m_animationPlayer.animationIDs;
			if( m_animationIndex >= animationIDs.length )
			{
				m_animationIndex = 0;
			}
			var newAnimationID : String = animationIDs[ m_animationIndex ];
			
			if( m_animationPlayer.currentAnimationID == newAnimationID )
			{
				playNextAnimation();
			}
			else
			{
				m_animationPlayer.play( newAnimationID );			
			}
		}
		
		private function handleFrameChange( a_event : FrameEvent ) : void
		{
			trace( "AnimationID", a_event.animationID, "FrameID", a_event.frameID );
		}
		
		private function handleAnimationChange( a_event : AnimationEvent ) : void
		{
			trace( "AnimationID", a_event.animationID, "Start", a_event.begin );
		}
		
	}
	
}